package com.wwwc.index.web.servlet;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.text.DecimalFormat;

import javax.servlet.ServletRequestWrapper;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.web.ejb.database.*;

public class ScheduleDData extends ScheduleD {
    public int sw = 0;
    public int sh = 0;

    public ScheduleDData(int w, int h) {
	this.sw = w;
	this.sh = h;
    }

    //==================================================================================
    // load 1099 data
    //==================================================================================

    public StringBuffer addScheduleDData (HttpServletRequest request, String call_path, 
					  String mid, String user_name, int user_level, 
					  boolean manager, boolean admin) {

	String aid = addCallBack;
	StringBuffer error = null;
	String year   = request.getParameter("year");
	String broker = request.getParameter("broker");

	if(year == null || broker == null) {
	    return getYearAndBroker(call_path, mid, aid, user_level, file_size_max);
	}
	
	ArrayList alist    = getRecords(user_name, broker, year);
	String broker_name = getBrokerName(broker);

	if(alist == null || alist.isEmpty()) {
	    error = loadData(request, call_path, mid, aid,  broker, year, user_name, user_level, file_size_max);
	    if(error == null) {
		if(vBean == null) {
		    vBean = new ScheduleDViewer();
		}
		
		error = vBean.main(request, call_path, mid, user_name, user_level, manager, admin);
		error.append("<BR><BR>Your year ["+year+"] data from ["+broker_name+"] has been saved.");
		error.append("<BR><BR>");
	    }
	    return error;
	}

	if(vBean == null) {
	    vBean = new ScheduleDViewer();
	}
	error = vBean.main(request, call_path, mid, user_name, user_level, manager, admin);
	error.append("<BR><BR>System found one record for year ["+year+"] of ["+broker_name+"] account.<BR>");
	error.append("<BR>If you have more than one Form-1099 from "+broker_name+",");
	error.append(" <BR><BR>you must use diffrent 3WCenter.com account.<BR><BR>"); 
	return error;
    }
    
    private ArrayList getRecords(String user_name, String broker, String year) {
	ArrayList alist = null;
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    alist = ejbdb.selectUserScheduleDInfo(user_name, broker, year);
	}
	catch(Exception e) {
	}
	return alist;
    }

    private StringBuffer loadData(HttpServletRequest request, String call_path, String mid, String aid, 
				  String broker, String year, String user_name, int user_level, int file_size_max) {
	HttpSession session = request.getSession(true);
	String error = null;
	StringBuffer sbf   = null;
	
	String t_sold    = null;
	String t_data    = null;
	String delimiter = null;
	
	Enumeration enum1 = null;
	Enumeration enum2 = null;
	String temp1 = null;
	String temp2 = null;

	Vector vv  = null;
	Vector vr  = null;
	Vector vt  = null;

	String submit = request.getParameter("B");
	
	if(submit != null) {
	    if(submit.equals("Test Data")) {
		t_sold    = request.getParameter("t_sold");
		t_data    = request.getParameter("t_data");
		delimiter = request.getParameter("delimiter");

		if(t_sold == null || t_sold.length()<1) {
		    error = "Please enter total sold.";
		}
		
		if(error == null) {
		    if(!MyUserInputCheck.checkMoney(t_sold, false)) {
			error = "Please check your total sold.";
		    }
		}
		
		if(error == null) {
		    if(t_data == null || t_data.length()<100) {
			error = "Please cut and paste your transaction file into the text area.";
		    }

		    if(error == null && t_data.length()> file_size_max*1000*user_level) {
			error = "Sorry! Your file size is over the "+file_size_max*user_level+"k limit.";
		    }
		}

		if(t_data.indexOf("<") != -1) {
		    t_data = t_data.replaceAll("<", "&lt;");
		}
		if(t_data.indexOf(">") != -1) {
		    t_data = t_data.replaceAll(">", "&gt;");
		}

		if(error == null) {
		    return assembleDataFile(request, call_path, mid, aid, error, broker, year, t_sold, t_data, file_size_max, delimiter);
		}
		else {
		    return(loadDataFile(call_path, mid, aid, error, broker, year, t_sold, t_data, file_size_max, delimiter));
		}
	    }

	    if(submit.equals("Update")) {
		Vector vrs = (Vector)session.getAttribute("ScheduleDDataRecords");
		t_sold    = request.getParameter("t_sold");

		Hashtable chs = new Hashtable();

		Enumeration enum = request.getParameterNames();
		String name  = null;
		String value = null;

		while(enum.hasMoreElements()) {
		    name = (String)enum.nextElement();
		    if(name != null && name.startsWith("column_")) {
			value = request.getParameter(name);
			if(value != null && !value.equals("0")) {
			    chs.put(name.replaceAll("column_",""), value);
			}
		    }
		}

		if(chs.size() != 6) {
		    if(chs.size()<6) {
			error = "Please select</I> [Buy/Sell][Tick][COST][SHARES][PRICE][DATE]<I>.";
		    }
		    if(chs.size()>6) {
			error = "Please select</I> [Buy/Sell][Tick][COST][SHARES][PRICE][DATE] <I> Only ONCE!";
		    }

		    return selectColumns(call_path, mid, aid, error, broker, year, t_sold, vrs, chs);
		}
		else {
		    Hashtable ck_hs = new Hashtable();
		    enum = chs.keys();
		    Object o = null;
		    while (enum.hasMoreElements()) {
			o = enum.nextElement();
			ck_hs.put(chs.get(o), o);
		    }
		    if(ck_hs.size() != chs.size()) {
			error = "Please select</I> [Buy/Sell][Tick][COST][SHARES][PRICE][DATE] <I> Only ONCE!";
			return selectColumns(call_path, mid, aid, error, broker, year, t_sold, vrs, chs);
		    }
		}
		return checkDataRecords(call_path, mid, aid, error, broker, year, user_name, t_sold, vrs, chs); 
	    }

	    if(submit.equals("Reset")) {
		t_sold = null;
		t_data = null;
		error = null;
	    }
	}

	return(loadDataFile(call_path, mid, aid, error, broker, year, t_sold, t_data, file_size_max, delimiter));
    }

    private StringBuffer checkDataRecords(String call_path, String mid, String aid, String error, String broker, 
					  String year, String user_name, String t_sold, Vector vrs, Hashtable shs) { 

	int buy_sell_idx = -1;
	int tick_idx     = -1;
	int cost_idx     = -1;
	int share_idx    = -1;
	int price_idx    = -1;
	int date_idx     = -1;

	String key   = null;
	String value = null;
	Enumeration enum = shs.keys();
	try {
	    while(enum.hasMoreElements()) {
		key = (String)enum.nextElement();
		value = (String)shs.get(key);
		if(value.equals("1")) {
		    buy_sell_idx = Integer.parseInt(key);
		}
		if(value.equals("2")) {
		    tick_idx = Integer.parseInt(key);
		}
		if(value.equals("3")) {
		    cost_idx = Integer.parseInt(key);
		}
		if(value.equals("4")) {
		    share_idx = Integer.parseInt(key);
		}
		if(value.equals("5")) {
		    price_idx = Integer.parseInt(key);
		}
		if(value.equals("6")) {
		    date_idx = Integer.parseInt(key);
		}
	    }
	}
	catch(Exception e) {
	    System.out.println("ScheduleDDate:checkDataRecords:Error:"+e);
	}

	int col = 0;
	Vector vr = null;

	int tb_share = 0;
	int ts_share = 0;

	double tb_cost = 0.0;
	double ts_cost = 0.0;
	double bs_cost = 0.0;

	String buy_sell = null;
	String tick     = null;
	String cost     = null;
	String share    = null;
	String price    = null;
	String date     = null;

	Vector vv = new Vector();
	Vector v  = null;

	int row = 0;
	enum = vrs.elements();
	while(enum.hasMoreElements()) {
	    row++;
	    vr = (Vector)enum.nextElement();
	    buy_sell = (String)vr.elementAt(buy_sell_idx);
	    tick     = (String)vr.elementAt(tick_idx);
	    cost     = (String)vr.elementAt(cost_idx);
	    share    = (String)vr.elementAt(share_idx);
	    price    = (String)vr.elementAt(price_idx);
	    date     = (String)vr.elementAt(date_idx);

	    if(!MyUserInputCheck.checkMoney(cost)) {
		error = "Error found at row ["+row+"] Column: [Cost] Value ["+cost+"]";
		break;
	    }
	    else {
		bs_cost = Double.parseDouble(cost);
		if(bs_cost >0) {
		    ts_cost += bs_cost; 
		}
		else {
		    tb_cost += bs_cost; 
		}
	    }

	    if(error == null && !MyUserInputCheck.checkMoney(share)) {
		error = "Error found at row ["+row+"] Column: [Shares] Value ["+share+"]";
		break;
	    }

	    if(error == null && !MyUserInputCheck.checkMoney(price)) {
		error = "Error found at row ["+row+"] Column: [Price] Value ["+price+"]";
		break;
	    }
	    /*
	    if(error == null) {
		if(Double.parseDouble(price)*Double.parseDouble(share) > java.lang.Math.abs(bs_cost)) {
		    System.out.println("DDDDDDDDDDD"+java.lang.Math.abs(bs_cost)+"BBBBBBBBBB"+bs_cost);
		    error = "Error found at row ["+row+"]";
		    break;
		}
	    }
	    */
	    
	    if(error == null) {
		if(date == null || date.indexOf(year) == -1) {
		    error = "Data is not for  year "+year+".";
		    break;
		}
	    }

	    if(error == null) {
		if(buy_sell.length()>1) {
		    buy_sell = buy_sell.toUpperCase();
		    if(buy_sell.startsWith("B")) {
			buy_sell = "B";
		    }
		    if(buy_sell.startsWith("S")) {
			buy_sell = "S";
		    }
		}
	    }

	    v = new Vector();
	    v.add(buy_sell);
	    v.add(cost);
	    v.add(tick);
	    v.add(share);
	    v.add(price);
	    v.add(date);
	    vv.add(v);
	}

	if(error == null) {
	    if(java.lang.Math.abs((Double.parseDouble(t_sold)-ts_cost))>1) {
		error = "Error Found! Total sold that you entered is: </I>$"+t_sold+"."+
		    "<I> Total sold from your data is not</I>:$"+t_sold+"<I>"+
		    "<BR><I> You can find total sold from your Form-1099 .</I>"+
		    "<BR><I> Your data has not been saved yet!!</I>";
	    }
	}
	
	if(error == null) {
	    if(!saveThisYearData(broker, year, user_name, ""+ts_cost, vv)) {
		error = "Save Data Error. Please try again.";
	    }
	    else {
		return null;
	    }
	}

	return selectColumns(call_path, mid, aid, error, broker, year, t_sold, vrs, shs);
    }

    private StringBuffer selectColumns(String call_path, String mid, String aid, String error, 
				       String broker, String year, String t_sold, Vector vrs, Hashtable shs) {

	StringBuffer sbf = new StringBuffer();
	sbf.append("<FORM method=post action="+call_path+">");
	sbf.append("<TABLE border=0>");
	sbf.append("<TR>");
	sbf.append("<TD align=center><B>Broker</B> ["+getBrokerName(broker)+"] <B>Year</B> ["+year+"]</TD>");
	sbf.append("</TR>");
	sbf.append("</TABLE><BR>");
	
	if(error != null) {
	    sbf.append("<img src=/images/star.gif><I>"+error+"</I><BR><BR>");
	}
	
	sbf.append("<TABLE style='font-size: 10pt' border=1 width=80%>");
	sbf.append("<TR>");
	sbf.append("<TD align=center>Total Sold On Form-1099 :$");
	sbf.append("<input type=text size=10 name=t_sold value='"+t_sold+"'></TD>");
	sbf.append("</TR>");
	
	sbf.append("<TR>");
	sbf.append("<TD align=center><input type=submit value='Update' name=B style='font-size: 8pt'>"); 
	sbf.append("<input type=submit value='Reset' name=B style='font-size: 8pt'></TD>"); 
	sbf.append("</TR>");

	sbf.append("<TR><TD align=center>");

	sbf.append("<table border=1 style='font-size:6pt'>");
	
	Enumeration enum1 = null;
	Enumeration enum2 = null;
	Vector vr = null;

	enum1 = vrs.elements();
	int m=0;
	String temp = null;
	int sidex = 0;

	while(enum1.hasMoreElements()) {
	    vr = (Vector)enum1.nextElement();
	    if(m==0) {
		sbf.append("<tr><td>Row</td>");
		for(int j=0; j<vr.size();j++){
		    sidex = 0;
		    if(shs != null) {
			temp = (String)shs.get(""+j);
			if(temp != null) {
			    sidex = Integer.parseInt(temp);
			}
		    }
		    sbf.append("<td>"+getColumnOptions("column_"+j, sidex)+"</td>");
		}
		sbf.append("</tr>");
	    }
	    
	    sbf.append("<tr><td>"+(m+1)+"</td>");
	    enum2 = vr.elements();
	    while(enum2.hasMoreElements()) {
		sbf.append("<td>"+enum2.nextElement()+"</td>");
	    }
	    sbf.append("</tr>");
	    m++;
	}
	
	sbf.append("</table>");

	sbf.append("</TD></TR>");
	sbf.append("<TR>");
	sbf.append("<TD align=center><input type=submit value='Update' name=B style='font-size: 8pt'>"); 
	sbf.append("<input type=submit value='Reset' name=B style='font-size: 8pt'></TD>"); 
	sbf.append("</TR>");
	sbf.append("</TABLE>");
	sbf.append("<input type=hidden name=year value="+year+">");
	sbf.append("<input type=hidden name=broker value="+broker+">");
	sbf.append("<input type=hidden name=mid value="+mid+">");
	sbf.append("<input type=hidden name=aid value="+aid+">");		
	sbf.append("</FORM>");
	return sbf;
    }

    private StringBuffer assembleDataFile(HttpServletRequest request, String call_path, String mid, String aid,  
					  String error, String broker, String year, String t_sold, String t_data, 
					  int file_size_max, String delimiter) {
	HttpSession session = request.getSession(true);
	Vector vr = null;
	Vector vrs = new Vector();

	if(delimiter == null) {
	    delimiter =",";
	}

	String temp1 = null;
	String temp2 = null;

	StringTokenizer stk1 = new StringTokenizer(t_data, "\n");
	StringTokenizer stk2 = null;
	boolean same_size = false;

	int row = 0;
	int col = 0;

	if(broker.equals("01")) { //Datek
	    while(stk1.hasMoreTokens()) {
		temp1 = stk1.nextToken();
		if(temp1 != null && (temp1.trim()).length()>0 && temp1.indexOf(".") != -1) {
		    col =0;
		    vr = new Vector();
		    stk2 = new StringTokenizer(temp1, delimiter);
		    while(stk2.hasMoreTokens()) {
			temp2 = stk2.nextToken();
			if(temp2==null || temp2.length()==0) {
			    //skip
			}
			else if(temp2.equals("@") && col==3) {
			    //skip
			}
			else {
			    vr.add(temp2);
			}
			col++;
		    }
		    
		    if(vr.size()>=6) {
			vrs.add(vr);
			same_size = true;
		    }
		}
		row++;
	    }
	}
	else {
	    while(stk1.hasMoreTokens()) {
		temp1 = stk1.nextToken();
		if(temp1 != null && (temp1.trim()).length()>0 && temp1.indexOf(".") != -1) {
		    vr = new Vector();
		    stk2 = new StringTokenizer(temp1, delimiter);
		    while(stk2.hasMoreTokens()) {
			temp2 = stk2.nextToken();
			vr.add(temp2);
		    }
		    
		    if(vr.size()>=6) {
			vrs.add(vr);
			same_size = true;
		    }
		}
	    }
	}

	// same_size was inited to true if found columns >=6

	int columns = 0;
	StringBuffer esbf = new StringBuffer("");
	Enumeration enum = vrs.elements();
	while(enum.hasMoreElements()) {
	    vr = (Vector)enum.nextElement();
	    if(columns ==0) {
		columns = vr.size();
		if(columns <6) {
		    esbf.append("<BR>Error:</I>"+vr.toString()+"<I>");
		    same_size = false;
		    break;
		}
	    }
	    else {
		if(vr.size() != columns) {
		    esbf.append("<BR>Error:</I>"+vr.toString()+"<I>");
		    same_size = false;
		    break;
		}
	    }
	}

	if(same_size) {
	    Hashtable chs = getBrokerRecordFormat(broker, vr);
	    session.setAttribute("ScheduleDDataRecords", vrs);
	    return selectColumns(call_path, mid, aid, error, broker, year, t_sold, vrs, chs);
	}
	else {
	    error = "Error Found in your data file."+esbf.toString();
	    return(loadDataFile(call_path, mid, aid, error, broker, year, t_sold, t_data, file_size_max, delimiter));
	}
    }

    private Hashtable getBrokerRecordFormat(String broker, Vector vr) {
	//Datek                == 1
	//Ameritrade           == 2
	//E*Trade              == 3
	//TD Waterhouse        == 4
	//Fidelity             == 5
	//Scottade             == 6
	//Charles Schwab & Co  == 7
	//Merrill Lynch Direct == 8
	//HARRISdirect         == 9

	String temp = null;
	Hashtable chs = null;
	int bid = Integer.parseInt(broker);

	if(vr == null || broker == null || bid >9 || bid <0) {
	    return null;
	}

	//=====================================================
	// Datek 
	//=====================================================

	if(bid == 1) {
	    if(vr.size() !=11) {
		return null;
	    } 
	    
	    temp = (String)vr.elementAt(3);
	    if(temp == null || !(temp.equals("B") || temp.equals("S"))) {
		return null;
	    }

	    temp = (String)vr.elementAt(6);
	    if(!MyUserInputCheck.checkMoney(temp)) {
		return null;
	    }

	    temp = (String)vr.elementAt(7);
	    if(!MyUserInputCheck.checkMoney(temp)) {
		return null;
	    }

	    temp = (String)vr.elementAt(8);
	    if(!MyUserInputCheck.checkMoney(temp)) {
		return null;
	    }

	    chs = new Hashtable();
	    chs.put("3","1");
	    chs.put("4","2");
	    chs.put("6","3");
	    chs.put("7","4");
	    chs.put("8","5");
	    chs.put("9","6");
	}

	return chs;
    }

    private StringBuffer loadDataFile (String call_path, String mid, String aid, 
				       String error, String broker, String year,
				       String t_sold, String t_data, int file_size_max, String delimiter) {
	StringBuffer sbf = new StringBuffer();

	if(t_sold == null) {
	    t_sold = "";
	}
	if(t_data == null) {
	    t_data = "";
	}

	sbf.append("<FORM method=post action="+call_path+">");
	sbf.append("<TABLE border=0>");
	sbf.append("<TR>");
	sbf.append("<TD align=center><B>Broker</B> ["+getBrokerName(broker)+"] <B>Year</B> ["+year+"]</TD>");
	sbf.append("</TR>");
	sbf.append("</TABLE><BR><BR>");
	
	if(error != null) {
	    sbf.append("<img src=/images/star.gif><I>"+error+"</I><BR><BR>");
	}

	sbf.append("<TABLE style='font-size: 10pt' border=1 width=80%>");

	sbf.append("<TR>");
	sbf.append("<TD align=center><B>Total Sold On Form-1099:</B>");
	sbf.append("$ &nbsp; <input type=text name=t_sold value="+t_sold+"></TD>");
	sbf.append("</TR>");

	sbf.append("<TR>");
	sbf.append("<TD align=center><B>Please cut and paste your [Year "+year+"] Transaction File into the text area</B>");
	sbf.append("<BR><I>(File size is limited to "+file_size_max+"k)</I></TD>");
	sbf.append("</TR>");

	sbf.append("<TR>");
	sbf.append("<TD align=center><TEXTAREA rows=10 cols=56 name=t_data>"+t_data+"</TEXTAREA></TD>");
	sbf.append("</TR>");

	if(delimiter != null) {
	    if(delimiter.indexOf("\"") != -1) {
		delimiter = delimiter.replaceAll("\"", "&quot;");
	    }
	    sbf.append("<TR>");
	    sbf.append("<TD align=center><B>Please enter your delimiter &nbsp;</B>");
	    sbf.append("<input type=text size=5 name=delimiter value=\""+delimiter+"\"></TD>");
	    sbf.append("</TR>");
	}

	sbf.append("<TR>");
	sbf.append("<TD align=center><input type=submit value='Test Data' name=B style='font-size: 8pt'>"); 
	sbf.append("<input type=submit value='Reset' name=B style='font-size: 8pt'></TD>"); 
	sbf.append("</TR>");

	sbf.append("</TABLE>");
	    
	sbf.append("<input type=hidden name=year value="+year+">");
	sbf.append("<input type=hidden name=broker value="+broker+">");
	sbf.append("<input type=hidden name=mid value="+mid+">");
	sbf.append("<input type=hidden name=aid value="+aid+">");		
	sbf.append("</FORM>");
	return sbf;
    }

    private boolean saveThisYearData(String broker, String year, String user_name, String t_sold, Vector vv) {
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    if(ejbdb.insertTaxScheduleD(user_name, broker, t_sold, year, vv)>0) {
		return true;
	    }
	}
	catch(Exception e) {
	    System.out.println("ScheduleD:saveThisYearData:Error:"+e);
	    return false;
	}
	return false;
    }
  

    private StringBuffer getColumnOptions(String name, int selected_index) {
	StringBuffer sbf = new StringBuffer();
	ArrayList a = new ArrayList();
	a.add("--------");
	a.add("Buy/Sell");
	a.add("Tick");
	a.add("Cost");
	a.add("Shares");
	a.add("Price");
	a.add("Date");
	sbf.append("<select name="+name+" style='font-size:6pt'>");
	Iterator it = a.iterator();
	int m=0;
	while(it.hasNext()) {
	    if(selected_index==m) {
		sbf.append("<option value="+m+" selected>"+it.next()+"</option>");
	    }
	    else {
		sbf.append("<option value="+m+">"+it.next()+"</option>");
	    }
	    m++;
	}
	sbf.append("</select>");
	return sbf;
    }

    /*
    public StringBuffer addViewEditTotalSold(HttpServletRequest request, String call_path, String mid, String aid, 
					     String broker, String year, String user_name) {
	StringBuffer error = null;
	String query = "SELECT * FROM TAX_SCHEDULED WHERE (USERNAME='"+user_name+
	    "' AND BROKER_ID='"+broker+"' AND YEAR="+year+") ORDER BY DATE;";
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }

	    ArrayList alist = ejbdb.select(query);
	    Iterator it = alist.iterator();
	    String f_id = null;
	    String data = null;
	    String t_sold = null;
	    ArrayList a = null;
	    while (it.hasNext()) {
		a      = (ArrayList)it.next();
		f_id   = (String)a.get(0);
		t_sold = (String)a.get(4);
	    }

	    return totalSold(request, call_path, mid, aid, broker, year, t_sold, user_name);
	}
	catch (Exception e) {
	}
	return null;
    }
    */
    /*
    public StringBuffer totalSold(HttpServletRequest request, String call_path, String mid, String aid, String broker, String year, 
				  String t_sold, String user_name) {

	if(t_sold == null) {
	    return addTotalSold(call_path, mid, aid, broker, year, request.getParameter("t_sold"), user_name);
	}

	return editTotalSold(call_path, mid, aid, broker, year, t_sold, user_name);
    }

    private StringBuffer editTotalSold(String call_path, String mid, String aid, String broker, String year, 
				       String t_sold, String user_name) {
	StringBuffer sbf   = new StringBuffer ();
	return sbf.append("Edit");
    }
    */
    /*
    private StringBuffer addTotalSold(String call_path, String mid, String aid, String broker, String year, 
				       String t_sold, String user_name) {
	String broker_name = getBrokerName(broker);
	StringBuffer sbf   = new StringBuffer ();
	StringBuffer error = null;
	if(t_sold != null) {
	    if(t_sold.length()==0) {
		error = new StringBuffer("Please enter total sold!");
	    }
	    if(error == null) {
		if(!MyUserInputCheck.checkDecimal(t_sold)) {
		    error = new StringBuffer("Error found at field [Total Sold].");
		}
	    }

	    if(error == null) {
		if(saveTotal(user_name, broker, year, t_sold, "0") == -1) {
		    error = new StringBuffer("Save data error.");
		}
		else {
		    return null;
		}
	    }
	}

	sbf.append("<BR><BR>");
	sbf.append("<TABLE style='font-size: 12pt' border=0>");
	sbf.append("<TR><TD>Please enter total sold on ["+broker_name+"] [Year-"+year+" Form-1099]</TD></TR>");

	if(error != null) {
	    sbf.append("<TR><TD style='font-size: 10pt' align='center'>");
	    sbf.append("<img src='/images/star.gif' border='0'><I>"+error+"</I>");
	    sbf.append("</TD></TR>");
	}
	sbf.append("</TABLE>");
	
	sbf.append("<FORM method=post action='"+call_path+"'>");
	sbf.append("<TABLE border=0 style='font-size: 10pt'>");
	sbf.append("<TR>");
	sbf.append("<TD>Total Sold On ["+broker_name+"] [Year-"+year+"] Form-1099 $</TD>");

	if(t_sold == null) {
	    sbf.append("<TD align=left><input type=text name=t_sold ");
	    sbf.append("  size=20 maxlength=19 style='font-size: 8pt'>");
	}
	else {
	    sbf.append("<TD align=left><input type=text name=t_sold value='"+t_sold+"'");
	    sbf.append("  size=20 maxlength=19 style='font-size: 8pt'>");
	}
	
	sbf.append("</TD>");
	sbf.append("</TR>");
	sbf.append("</TABLE>");
	
	sbf.append("<BR><BR>");
	sbf.append("<TABLE style='font-size: 8pt' border=0>");
	sbf.append("<TR>");
	sbf.append("<TD>");
	sbf.append("<input type=submit value=Next name=B1 style='font-size: 8pt'>");
	sbf.append("<input type=hidden name=year value="+year+">");
	sbf.append("<input type=hidden name=mid value="+mid+">");
	sbf.append("<input type=hidden name=aid value="+aid+">");
	sbf.append("<input type=hidden name=broker value="+broker+">");
	sbf.append("</TD>");
	sbf.append("</TR>");
	sbf.append("</TABLE>");
	sbf.append("</FORM>");
	return sbf;
    }
    */
    /*
    public int saveTotal(String username, String broker, String year, String t_sold, String t_cost) {
	int rs = -1;
	if(username == null || username.equals("guest")) {
	    return rs;
	}

	query = "INSERT INTO TAX_SCHEDULED VALUES(null,'"+username+"','"+broker+"',"+year+","+t_sold+","+t_cost+",'now','');";
	try {
	    if (ejbdb == null) {
		ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	    }
	    rs = ejbdb.insert(query);
	}
	catch (Exception e) {
	    return rs;
	}
	return rs;
    }
    */
    /*
	String year   = request.getParameter("year");

	if (year == null) {
	    out.println(MyMessage.localHtmlMessage("Error:101",user_ip, local_ips));
	    return;
	}

	
	DatabaseAccess db = new DatabaseAccess();
	String query      = null;
	Vector v          = null;

	if(action.equals("find_1099_info")) {
	    FieldRecord20 r = new FieldRecord20();
	    query = "SELECT * FROM tax_sch_d_info WHERE (FIELD2='"+user_name+
		"' AND FIELD3='"+year+"') ORDER BY FIELD5;";
	    v = new Vector();
	    db.select(query);
	    v = db.getTableRecordVector();

	    if(v.size()==0) {
		String forward = "/servlet/ScheduleDDataLoader?action=enter_1099_total&year="+year+"&form_id=1"; 
		RequestDispatcher rd = request.getRequestDispatcher(forward);
		rd.forward(request,response);
		return;
	    }

	    Enumeration enum = v.elements();
	    while(enum.hasMoreElements()) {
		r = new FieldRecord20((Vector)enum.nextElement());
		if(r.getField7() == null || r.getField7().length()<1) {
		    String forward = "/servlet/ScheduleDDataLoader?action=load_1099_data&year="+year+"&f_id="+r.getField1()+
			"&money="+r.getField4()+"&form_id="+r.getField5(); 
		    RequestDispatcher rd = request.getRequestDispatcher(forward);
		    rd.forward(request,response);
		    return;
		} 
	    }

	    String forward = "/servlet/ScheduleDDataLoader?action=main_page&year="+year+"&f_id="+r.getField1()+
		"&money="+r.getField4()+"&form_id="+r.getField5(); 
	    RequestDispatcher rd = request.getRequestDispatcher(forward);
	    rd.forward(request,response);
	    return;
	}

	if(action.equals("main_page")) {
	    out.println("<HTML><HEAD><TITLE></TITLE>");
	    out.println("</HEAD><BODY><CENTER>");
	    
	    query = "SELECT * FROM tax_sch_d_info WHERE (FIELD2='"+user_name+
		"' AND FIELD3='"+year+"') ORDER BY FIELD20, FIELD5;";
	    
	    v = new Vector();
	    db.select(query);
	    v = db.getTableRecordVector();

	    if(v.size()==0) {
		String forward = "/servlet/ScheduleDDataLoader?action=enter_1099_total&year="+year+"&form_id=1"; 
		RequestDispatcher rd = request.getRequestDispatcher(forward);
		rd.forward(request,response);
		return;
	    }

	    FieldRecord20 r = null;
	    Enumeration enum = v.elements();

	    while(enum.hasMoreElements()) {
		r = new FieldRecord20((Vector)enum.nextElement());
		out.println("<TABLE style='font-size: 14pt' border=0 width=600>");
		out.println("<TR>");
		out.println("<TD align=center> Year "+r.getField3()+" Form-1099-"+r.getField5()+"</TD>");
		out.println("</TR>");
		out.println("</TABLE><BR><BR>");

		out.println("<TABLE style='font-size: 10pt' border=1 width=600>");
		out.println("<TR>");
		out.println("<TD>Total Sold (You Entered)</TD>");
		out.println("<TD align=right>$"+r.getField4()+"</TD>");

		if(r.getField7() != null && r.getField7().length()>2) {
		    out.println("<TD align=center>OK</TD></TR>");
		}
		else {
		    out.println("<FORM method=post action=ScheduleDDataLoader>");
		    out.println("<TD align=center><input type=submit value='Edit Total' name=B1 ");
		    out.println(" style='font-size: 8pt'>");
		    out.println("<input type=hidden name=f_id value="+r.getField1()+">");	
		    out.println("<input type=hidden name=form_id value="+r.getField5()+">");	
		    out.println("<input type=hidden name=year value="+year+">");
		    out.println("<input type=hidden name=money value="+r.getField4()+">");
		    out.println("<input type=hidden name=action value=edit_1099_total>");			
		    out.println("</TD></TR>");
		    out.println("</FORM>");
		}

		out.println("<TR>");
		out.println("<TD>Total Sold (From Your Data)</TD>");

		if(r.getField7() != null && r.getField7().length()>2) {
		    out.println("<TD align=right>$"+r.getField7()+"</TD>");
		    out.println("<TD align=center>OK</TD></TR>");
		}
		else {
		    out.println("<TD>&nbsp</TD>");
		    out.println("<FORM>");
		    out.println("<TD align=center><input type=submit value='Load Data' name=B1 style='font-size: 8pt'></TD></TR>");
		    out.println("<input type=hidden name=f_id value="+r.getField1()+">");	
		    out.println("<input type=hidden name=form_id value="+r.getField5()+">");	
		    out.println("<input type=hidden name=year value="+year+">");
		    out.println("<input type=hidden name=money value="+r.getField4()+">");
		    out.println("<input type=hidden name=action value=load_1099_data>");	
		    out.println("</FORM>");
		    out.println("</TABLE><BR><BR>");
		    out.println("</CENTER></BODY></HTML>");  
		    return;
		}

		out.println("<TR>");
		out.println("<TD>Total Bought</TD>");
		out.println("<TD align=right>$"+r.getField8()+"</TD>");
		out.println("<TD align=center>OK</TD></TR>");

		if(r.getField12() != null && r.getField12().equals("data_saved") && 
		   r.getField13() != null && r.getField13().length()>1 && 
		   r.getField14() != null && r.getField14().length()>1) {
		    out.println("<TR>");
		    out.println("<TD>Gain/Loss</TD>");
		    out.println("<TD align=right>$");
		    out.println(td.format(Double.parseDouble(r.getField14())-Double.parseDouble(r.getField13()))+"</TD>");
		    out.println("<TD align=center>OK</TD></TR>");
		}

		out.println("</TABLE>");
		out.println("<BR><BR>");
	    }

	    out.println("<TABLE style='font-size: 8pt'>");
	    out.println("<TR>");

	    if(r.getField12() != null && r.getField12().equals("data_saved")) {
		out.println("<TD>");
		out.println("<FORM method=post action=ScheduleDDataLoader>");
		out.println("<input type=submit name=B1 value='Add Form 1099'");
		out.println(" style='font-size: 8pt'>");
		out.println("<input type=hidden name=year value="+year+">");
		out.println("<input type=hidden name=action value=enter_1099_total>");
		out.println("<input type=hidden name=form_id value="+(1+Integer.parseInt(r.getField5()))+">");
		out.println("</FORM>");
		out.println("</TD>");
	    }
	    
	    out.println("<TD>");
	    out.println("<FORM method=post action=ScheduleDFindHolding>");
	    out.println("<input type=submit name=B1 value=Next ");
	    out.println(" style='font-size: 8pt'>");
	    out.println("<input type=hidden name=year value="+year+">");
	    out.println("</FORM>");
	    out.println("</TD>");
	    out.println("</TR></TABLE>");
	    out.println("</CENTER></BODY></HTML>");  
	    return;
	}
    */
  

    ///////////////////////////////////////////////////////////////////////
    // data input form
    ///////////////////////////////////////////////////////////////////////
    /*
    public StringBuffer getLoadDataForm (String call_path, String mid, String aid, String fid, String year, String t_sold) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<TABLE>");
	sbf.append("<TR><TD align=center style='font-size: 12pt'>");
	sbf.append("Please Cut and Paste your year ["+year+"] Transaction file into the Tax area!");
	sbf.append("</TD></TR>");
	sbf.append("<TR><TD align=center style='font-size: 10pt'>");
	sbf.append("<I> * Please make sure the total sold on this form is [<B>$"+t_sold+"</B>]</I>");
	sbf.append("</TD></TR>");
	sbf.append("</TABLE>");

	sbf.append("<FORM name=data_f method=post action="+call_path+">");
	sbf.append("<TABLE><TR><TD>");
	sbf.append("<textarea rows=10 name=input_data cols=70></textarea>");
	sbf.append("</TD></TR>");
	sbf.append("</TABLE>");

	sbf.append("<TABLE><TR><TD>");
	sbf.append("<input type=submit value=Continue name=B1 style='font-size: 8pt'>");
	sbf.append("</TD><TD>");
	sbf.append("<input type=reset value=Reset name=B1 style='font-size: 8pt'>");
	sbf.append("</TD></TR>");
	sbf.append("</TABLE>");
	sbf.append("<input type=hidden name=mid value="+mid+">");
	sbf.append("<input type=hidden name=aid value="+aid+">");
	sbf.append("<input type=hidden name=fid value="+fid+">");		
	sbf.append("</FORM>");
	return sbf;
    }
    */
}









